<?php
namespace Admin\Controller;

class MemberController extends CommonController
{

    /**
     * 列表
     *
     * @param  void
     * @return void
     */
    public function index()
    {
        if (IS_POST) {
            $this->delete();
        } else {
            $get = I('get.');
            $where = array();
            if (! empty($get['keyword'])) {
                $where['nickname|openid'] = array( 'like', "%{$get['keyword']}%" );
            }
            if (! empty($get['start_subscribe_time'])) {
                $where['subscribe_time'] = array( 'egt' , strtotime("{$get['start_subscribe_time']} 00:00:00") );
            }
            if (! empty($get['end_subscribe_time'])) {
                $where['subscribe_time'] = array( 'elt' , strtotime("{$get['end_subscribe_time']} 23:59:59") );
            }

            if (! empty($get['start_subscribe_time']) && ! empty($get['end_subscribe_time'])){
                $where['subscribe_time'] = array('between', array(strtotime("{$get['start_subscribe_time']} 00:00:00"), strtotime("{$get['end_subscribe_time']} 23:59:59")));
            }

            if (isset($get['vip']) && $get['vip'] ==! '') {
                $where['vip'] = $get['vip'];
            }

            if (! empty($get['channel'])) {
                $where['channel'] = 'qrscene_' . $get['channel'];
            }
            if (! empty($get['order_count'])) {
                switch ($get['order_count'])
                {
                    case 1;
                        $where['order_count'] = array('between', '1,3');
                        break;
                    case 2;
                        $where['order_count'] = array('between', '3,10');
                        break;
                    case 3;
                        $where['order_count'] = array( 'elt' , 10 );
                        break;
                }
            }


            $where['status'] = 1;

            $list = $this->lists(M('Member'), true, $where, 'subscribe_time DESC', 20);
            $channel = getChannel(false);
            $this->assign('channel', $channel);
            $this->assign('list', $list);
            $this->assign('get', $get);
            $this->display();
        }
    }

    /**
     * 修改
     *
     * @param void
     * @return void
     */
    public function edit()
    {
        if (IS_POST) {
            $data = I('post.');
            $result = D('Member')->update($data);
            if ($result) {
                $this->success('保存成功', U('index'));
            } else {
                $this->error('保存失败');
            }
        } else {
            $id = I('get.id');
            if (empty($id)) {
                $this->error('无效的数据');
            }
            $info = M('Member')->where('member_id = ' . $id)->find();
            $this->assign('info', $info);
            $this->display();
        }
    }

    /**
     * 删除
     *
     * @param void
     * @return void
     */
    public function delete()
    {
        $id = array_unique((array) I('id', 0));
        if (empty($id)) {
            $this->error('请选择要操作的数据!');
        }
        $where = array( 'member_id' => array( 'in', $id ) );
        $data = array( 'status' => 0, 'update_time' => time() );
        $result = M('Member')->where($where)->save($data);
        if ($result) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

}